#include "QyGLSky.h"

using namespace QyGL;

SkyBox::SkyBox( const Texture & topTexture, const Texture & bottomTexture,
	const Texture & leftTexture, const Texture & rightTexture,
	const Texture & frontTexture, const Texture & backTexture )
{

	objs[Top].setTexture(topTexture);
	objs[Bottom].setTexture(bottomTexture);
	objs[Left].setTexture(leftTexture);
	objs[Right].setTexture(rightTexture);
	objs[Front].setTexture(frontTexture);
	objs[Back].setTexture(backTexture);

}

void SkyBox::paint() {

	for ( int i = 0; i < 6; ++i )
		objs[i].paint();

}

void SkyBox::updateBounding() {


	QyMath::Point max = boundingMax();
	QyMath::Point min = boundingMin();
	VertexVector vertexes(4);
	int i = -1;

	// bottom
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], min.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 1, 1 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], max.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 1, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], max.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 0, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], min.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 0, 1 ) );
	objs[Bottom].setVertexes(vertexes);
	objs[Bottom].setType(GL_QUADS);
	objs[Bottom].setPaintFlags(Object::TexCoords|Object::Textured);

	// top
	i = -1;
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], min.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 1, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], min.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 0, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], max.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 0, 1 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], max.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 1, 1 ) );
	objs[Top].setVertexes(vertexes);
	objs[Top].setType(GL_QUADS);
	objs[Top].setPaintFlags(Object::TexCoords|Object::Textured);

	// back
	i = -1;
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], min.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 1, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], min.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 0, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], min.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 0, 1 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], min.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 1, 1 ) );
	objs[Back].setVertexes(vertexes);
	objs[Back].setType(GL_QUADS);
	objs[Back].setPaintFlags(Object::TexCoords|Object::Textured);

	// front
	i = -1;
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], max.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 0, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], max.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 0, 1 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], max.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 1, 1 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], max.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 1, 0 ) );
	objs[Front].setVertexes(vertexes);
	objs[Front].setType(GL_QUADS);
	objs[Front].setPaintFlags(Object::TexCoords|Object::Textured);

	// right
	i = -1;
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], min.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 1, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], max.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 0, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], max.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 0, 1 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( max.p[0], min.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 1, 1 ) );
	objs[Right].setVertexes(vertexes);
	objs[Right].setType(GL_QUADS);
	objs[Right].setPaintFlags(Object::TexCoords|Object::Textured);

	// left
	i = -1;
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], min.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 0, 0 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], min.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 0, 1 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], max.p[1], max.p[2] ), QyMath::Point(), QyMath::Point( 1, 1 ) );
	vertexes[++i] = QyGL::Vertex( QyMath::Point( min.p[0], max.p[1], min.p[2] ), QyMath::Point(), QyMath::Point( 1, 0 ) );
	objs[Left].setVertexes(vertexes);
	objs[Left].setType(GL_QUADS);
	objs[Left].setPaintFlags(Object::TexCoords|Object::Textured);

}
